﻿using B.Auth.APi.Application.Command.Menus;
using B.Auth.APi.Application.Command.Menus.Dto;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Menuss
{
    public class MenusShowCommandHand : IRequestHandler<MenusShowCommand, ApiResult<List<MenuDto>>>
    {

        private readonly IBaseRepository<MenuModel> _menu;

        public MenusShowCommandHand(IBaseRepository<MenuModel> menu)
        {
            _menu = menu;
        }

        public Task<ApiResult<List<MenuDto>>> Handle(MenusShowCommand request, CancellationToken cancellationToken)
        {
            var MenuDto = new List<MenuDto>();
            var menulist = _menu.GetAll().ToList();
            var result = new ApiResult<List<MenuDto>>();

            foreach (var menu in menulist)
            {
                MenuDto.Add(new()
                {
                    Id = menu.Id,
                    MenuName = menu.MenuName,
                    PID = menu.PID,
                    Url = menu.Url,
                    Sort = menu.Sort,
                    ParentMenuName = _menu.GetAll().FirstOrDefault(x => x.Id == menu.PID)?.MenuName==null?"顶级菜单" : _menu.GetAll().FirstOrDefault(x => x.Id == menu.PID).MenuName,
                    Del=menu.MenuDel,
                    CreateUserName=menu.CreateUserName,
                    UodateUserName=menu.UodateUserName,
                    CreateTime=menu.CreateTime,
                    UpdateTime=menu.UpdateTime





                });


                
            }
            result.data = MenuDto.OrderByDescending(x => x.Id).Where(x => x.Del == 0).ToList();

            return Task.FromResult(result);
            

          
        }
    }
}
